package cj.com.chabaike.dao;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;


import java.util.ArrayList;
import java.util.List;

import cj.com.chabaike.entity.HomeData;
import cj.com.chabaike.entity.NewsData;
import cj.com.chabaike.entity.seach.SeachData;
import cj.com.chabaike.entity.special.SpecialData;
import cj.com.chabaike.entity.special.Special_detail_data;

/**
 * Created by CJLAB on 2015/8/10.
 */
public class FetchJSONDAO {

    private  FetchJSONDAO() {}
    private static HttpUtils httpUtils;
    private static FetchJSONDAO fetchJSONDAO;
    private HomeData homeData=null;
    private NewsData newsData =null;
    private SeachData seachData =null;
    private  SpecialData specialData = null;
    private  Special_detail_data special_detail_data = null;


    public static FetchJSONDAO getFetchJSONDAOInstance() {
        if (fetchJSONDAO == null) {
            fetchJSONDAO = new FetchJSONDAO();
            httpUtils = new HttpUtils();
        }
        return fetchJSONDAO;
    }



    public HomeData getHomeData(String url, final ResultBack resultBack){


        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {

            @Override
            public void onStart() {
                Log.e("XUtils 线程:",Thread.currentThread().getName());
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                homeData = JSON.parseObject(responseInfo.result,HomeData.class);
                //Log.e("获取成功",homeData.toString());
                resultBack.resultWhat(true,homeData);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.e("获取失败","获取失败");
                resultBack.resultWhat(false,null);
            }
        });




        return homeData;
    }


    public void getNewsData(String url, final NewsDataResultBack newsDataResultBack){
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                newsData = JSON.parseObject(responseInfo.result,NewsData.class);
                Log.e("获取成功",newsData.toString());
                newsDataResultBack.newsResultWhat(true, newsData);

            }

            @Override
            public void onFailure(HttpException e, String s) {
                newsDataResultBack.newsResultWhat(false, null);
                Log.e("获取失败", "newsData获取失败,Http异常 = " + e.getMessage());
            }
        });


    }


public List<String> ImgurlArarryToList(Object Imgurl){
  JSONArray jsonArray = JSON.parseArray(Imgurl.toString());
    List<String> list = new ArrayList<String>();
  for(int i=0;i<jsonArray.size();i++){
      list.add(jsonArray.getJSONObject(i).getString("picimg"));
     // Log.w("解析层:","ImgUrl解析:" + jsonArray.getJSONObject(i).getString("picimg"));
  }
    return list;

}

    public void getSeachData(final String url, final SeachDataResultBack seachDataResultBack){
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                seachData = JSON.parseObject(responseInfo.result,SeachData.class);
                Log.e("获取成功",url);
                Log.e("获取成功",seachData.toString());
                seachDataResultBack.seachResultWhat(true,seachData);

            }

            @Override
            public void onFailure(HttpException e, String s) {
                seachDataResultBack.seachResultWhat(false,null);
                Log.e("获取失败", "seachData获取失败,Http异常 = " + e.getMessage());
            }
        });


    }

    public void getSepcialData(final String url, final SpecialDataResultBack specialDataResultBack){
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                specialData = JSON.parseObject(responseInfo.result,SpecialData.class);
                Log.e("获取成功",url);
                Log.e("获取成功",specialData.toString());
                specialDataResultBack.specialResultWhat(true, specialData);

            }
            @Override
            public void onFailure(HttpException e, String s) {
                specialDataResultBack.specialResultWhat(false, null);
                Log.e("获取失败", "specialData获取失败,Http异常 = " + e.getMessage());
            }
        });


    }

    public void getSepcialDetailData(final String url, final SpecialDataDetailResultBack specialDataDetailResultBack){
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                special_detail_data = JSON.parseObject(responseInfo.result,Special_detail_data.class);
                Log.e("获取成功",url);
                Log.e("获取成功",special_detail_data.toString());
                specialDataDetailResultBack.specialDetailResultWhat(true, special_detail_data);

            }
            @Override
            public void onFailure(HttpException e, String s) {
                specialDataDetailResultBack.specialDetailResultWhat(false, null);
                Log.e("获取失败", "specialData获取失败,Http异常 = " + e.getMessage());
            }
        });


    }




    public static interface  ResultBack{

        void resultWhat(boolean isSuccusful,HomeData homeData);


    }

    public static interface  NewsDataResultBack {
        void newsResultWhat(boolean isSuccusful, NewsData newsData);
    }

    public static interface  SeachDataResultBack {
        void seachResultWhat(boolean isSuccusful, SeachData seachData);
    }

    public static interface SpecialDataResultBack {
        void specialResultWhat(boolean isSuccusful, SpecialData specialData);
    }
    public static interface SpecialDataDetailResultBack {
        void specialDetailResultWhat(boolean isSuccusful, Special_detail_data special_detail_dataData);
    }
}
